package com.tom.zx.service.impl;

import com.tom.zx.entity.DecorationScheme;
import com.tom.zx.entity.DecorationSchemePicture;
import com.tom.zx.mapper.DecorationSchemeMapper;
import com.tom.zx.service.IDecorationSchemePictureService;
import com.tom.zx.service.IDecorationSchemeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tom.zx.vo.DecorationSchemeVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 装修方案 服务实现类
 *
 * @author lp
 * @since 2022-04-09
 */
@Service
public class DecorationSchemeServiceImpl
    extends ServiceImpl<DecorationSchemeMapper, DecorationScheme>
    implements IDecorationSchemeService {

  @Autowired private IDecorationSchemePictureService decorationSchemePictureService;

  @Override
  @Transactional(rollbackFor = Exception.class)
  public boolean saveOrUpdate(DecorationSchemeVo decorationScheme) {
    DecorationScheme scheme = new DecorationScheme();
    BeanUtils.copyProperties(decorationScheme, scheme);
    saveOrUpdate(scheme);
    decorationScheme.getDecorationSchemePictureList().stream()
        .peek(
            item -> {
              item.setDecorationSchemeId(scheme.getId());
            })
        .forEach(decorationSchemePictureService::saveOrUpdate);
    return true;
  }
}
